<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;

class AdminController extends CommonController
{
    //
    public function index()
    {

        //withTrashed 包换软删除
        $data=Admin::orderBy('id','desc')->withTrashed()->paginate($this->page);
        return view('admin.admin.index',compact('data'));
    }
    //添加显示
    public function create(){
        $data=Role::all();
        return view('admin.admin.create',compact('data'));
    }
    //添加处理
    public function store(Request $request){
        $post=$this->validate($request,[
           'username'=>'required|unique:admins,username',
            'truename'=>'required',
            'password'=>'required|confirmed',
            'phone' =>'required|phone',
            'email'=>'required|email',
            'roles_id'=>'required'
        ]);
        $model=Admin::create($post);
        $password=$post['password'];
        //发送通知邮件
        \Mail::send('email.adminUser',compact('model','password'),function (Message $message) use ($model) {
            $message->to($model->email);
            //主题名
            $message->subject('学校管理后台账号开通通知');
        });
        //日志
        return redirect(route('admin.admin.index'))->with(['error'=>'添加用户'.$post['username'].'成功']);
    }
    //删除用户
    public function del(int $id){
      Admin::find($id)->delete();
       return ['status'=>0,'msg'=>'删除成功'];
    }
    //恢复删除的用户处理
    public function restore(int $id)
    {
        Admin::withTrashed()->find($id)->restore();
        return redirect(route('admin.admin.index'))->with(['error'=>'恢复用户成功']);
    }
    public function delall(Request $request)
    {
        $ids=$request->get('id');
        //
        Admin::destroy($ids);
        return ['status'=>0,'msg'=>'删除成功'];
    }
    //修改显示
    public function edit(int $id){
        $data=Admin::find($id);
        return view('admin.admin.edit',compact('data'));
    }
    //修改管理员资料处理
    public function update(Request $request,int $id){
        $model=Admin::find($id);
        $post=$this->validate($request,[
            'username'=>'required',
            'truename'=>'required',
            'phone' =>'required|phone',
            'email'=>'required|email',
        ]);
        try {
            $model->update($post);
            //发送邮件
            //写入日志
            return redirect(route('admin.admin.index'))->with(['error'=>'修改管理员资料成功']);
        }catch (\Illuminate\Database\QueryException $queryException){
            //写入日志
            return redirect(route('admin.admin.edit',$id))->with(['error'=>'修改失败，可能账号已经存在']);
        }

    }
    public function role(Request $request,Admin $admin){
        if ($request->isMethod('post')){
         $post=$this->validate($request,[
             'roles_id'=>'required',
         ],[
             'roles_id.required'=>'权限不能不选'
         ]);
         $admin->update($post);
            return redirect(route('admin.admin.index'))->with(['error'=>'分配权限成功']);
        }
        $roleAll=Role::all();
        return view('admin.admin.role',compact('admin','roleAll'));
    }
    //修改自己的密码
    //修改自己的信息

}
